package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwy.dao.BookMessageMapper;
import com.lwy.dao.BookUserMapper;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookUser;
import com.lwy.service.BookUserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//状态表service接口实现类
@Service
public class BookUserServiceImpl extends ServiceImpl<BookUserMapper, BookUser> implements BookUserService {

//    @Resource
//    BookUserService bookUserService;

    //    获取用户密码
    public String getPwd(String password) {
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        return bc.encode(password);
    }

    //    修改用户密码
    public Object updPwd(String password, Integer id) {
        String encode = getPwd(password);
        BookUser bookUser = new BookUser();
        bookUser.setPassword(encode);
        bookUser.setId(id);
        int i = baseMapper.updateById(bookUser);
        if (i > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    public Integer logging(String username, String password) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        List<BookUser> username1 = baseMapper.selectList(new QueryWrapper<BookUser>().eq("username", username));
        if (username1.size() > 0) {
            return 3;
        } else {
            BookUser bookUser = new BookUser();
            bookUser.setUsername(username);
            bookUser.setPassword(getPwd(password));
            bookUser.setHeadimgurl("1.png");
            long a = 1;
            bookUser.setStatus((byte) a);
            bookUser.setIdentity(0);
            bookUser.setCreate_time(format);
            bookUser.setUpdate_time(format);
            bookUser.setUp(1);
            bookUser.setRecommend(1);
            int insert = baseMapper.insert(bookUser);
            if (insert > 0) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    //    获取用户密码
    public Integer getPassword(String pwd, Integer id) {
        BookUser bookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().eq("id", id));
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        boolean b = bc.matches(pwd, bookUser.getPassword());
        if (b) {
            return 1;
        } else {
            return 2;
        }
    }

        public PageInfo<BookUser> page(String username,Integer pageNum, Integer pageSize){
            if(null==pageNum){
                pageNum=1;
            }if(null!=username&&!"".equals(username)){
                username = "%"+username+"%";
            }if(null==pageSize){
                pageSize=5;
            }
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<BookUser> pageInfo = new PageInfo<BookUser>(baseMapper.selectByUserName(username));
            return  pageInfo;
        }
    }
